﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Gateway01;

public partial class OnlinePay_Alipay_Send2 : PageBase
{
    private string gateway = "";
    private string service = "";
    private string partner = "";
    private string sign_type = "";
    private string out_trade_no = "";
    private string subject = "";
    private string body = "";
    private string payment_type = "";
    private string total_fee = "";
    private string show_url = "";
    private string seller_email = "";
    private string key = "";
    private string return_url = "";
    private string _input_charset = "";
    private string notify_url = "";
    private string buyer_email = "";
    private string AlipayFormalitiesFees = "";
    private string Royalty = "";
    private string OnlinePayAlipayFormalitiesFeesSign = "";
    private string paymethod = "";
    private string ServiceType = "";
    private string agentID = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        bool OnlinePay_Alipay_Status_ON = PublicFunction.GetOptionsAsBoolean("OnlinePay_Alipay_Status_ON", false);

        if (!OnlinePay_Alipay_Status_ON)
        {
            PublicFunction.GoError(ErrorNumber.Unknow, "未启用此功能", this.GetType().BaseType.FullName);

            return;
        }

        this.PayMoney.Enabled = false;
        double money = 0;

        if (!IsPostBack)
        {
            money = ShoveEL._Convert.StrToDouble(Request["PayMoney"], 0);
            if (money < 1)
            {
                this.PayMoney.Enabled = true;
                OnlinePayed.Enabled = true;
                bt_Submit.Enabled = false;

                ShoveEL._Web.JavaScript.Alert(this.Page, "请输入正确的充值金额！");
                return;
            }
            else
            {
                this.PayMoney.Enabled = false;
                OnlinePayed.Enabled = false;
                bt_Submit.Enabled = true;
            }

            labBalance.Text = "现存入：<font color='red'>" + money.ToString("N") + "</font> 元，账户余额：<font color='red'>" + _User.Balance.ToString("N") + "</font> 元";

            double FormalitiesFeesScale = PublicFunction.GetOptionsAsDouble("OnlinePay_Alipay_PayFormalitiesFeesScale", 0) / 100;
            double FormalitiesFees = Math.Round(money * FormalitiesFeesScale, 2);

            money += FormalitiesFees;
            this.PayMoney.Enabled = true;
            this.PayMoney.Text = money.ToString();
            this.tbFormalitiesFees.Text = FormalitiesFees.ToString();
            labFormalitiesFees.Text = "手续费 " + FormalitiesFees.ToString() + " 元由支付网关提供商收取。";
            //labBalance.Text += " " + (labFormalitiesFees.Text != "" ? labFormalitiesFees.Text : "");

            this.PayMoney.Enabled = false;
            bt_Submit.Enabled = true;

            BindDataForPayList();

            string Description = "金额：" + (money - FormalitiesFees).ToString() + " " + (labFormalitiesFees.Text != "" ? labFormalitiesFees.Text : "");
            lab1.Text = Description;
            lab5.Text = Description;
            lab6.Text = Description;
            lab7.Text = Description;
            lab8.Text = Description;
            lab9.Text = Description;
            lab10.Text = Description;
            lab11.Text = Description;
            lab12.Text = Description;
            lab13.Text = Description;
            lab14.Text = Description;
            lab15.Text = Description;
            lab16.Text = Description;
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        RequestLoginPage = "BalanceCenter/../OnlinePay/Alipay/Send2.aspx";

        base.OnInit(e);
    }

    #endregion

    private void BindDataForPayList()
    {
        double Money = double.Parse(PayMoney.Text) - double.Parse(tbFormalitiesFees.Text);
    }

    protected void OnlinePayed_Click(object sender, System.EventArgs e)
    {
        double money = 0;

        money = ShoveEL._Convert.StrToInt(this.PayMoney.Text.Trim(), 0);
        if (money < 1)
        {
            this.PayMoney.Enabled = true;
            OnlinePayed.Enabled = true;
            bt_Submit.Enabled = false;

            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入正确的充值金额！");
            return;
        }
        else
        {
            this.PayMoney.Enabled = false;
            OnlinePayed.Enabled = false;
            bt_Submit.Enabled = true;
        }

        double FormalitiesFeesScale = PublicFunction.GetOptionsAsDouble("OnlinePay_Alipay_PayFormalitiesFeesScale", 0) / 100;
        double FormalitiesFees = Math.Round(money * FormalitiesFeesScale, 2);

        money += FormalitiesFees;
        this.PayMoney.Enabled = true;
        this.PayMoney.Text = money.ToString();
        this.tbFormalitiesFees.Text = FormalitiesFees.ToString();
        labFormalitiesFees.Text = "手续费 " + FormalitiesFees.ToString() + " 元由支付网关提供商收取。";

        this.PayMoney.Enabled = false;
        OnlinePayed.Enabled = false;
        bt_Submit.Enabled = true;
    }

    private void AlipayParameter()
    {
        //业务参数赋值；
        gateway = "https://www.alipay.com/cooperate/gateway.do?";	                //支付接口
        service = "create_direct_pay_by_user";
        sign_type = "MD5";
        payment_type = "1";                                                          //支付类型
        _input_charset = "utf-8";                                                    //编码类型
        //return_url = "http://" + _Site.Urls.Split(',')[0] + "/OnlinePay/Alipay/Receive.aspx"; //服务器通知返回接口
        //notify_url = "http://" + _Site.Urls.Split(',')[0] + "/OnlinePay/Alipay/Notify.aspx";  //服务器通知返回接口(暂时不用)
        return_url = ShoveEL._Web.Utility.GetSiteUrl() + "/OnlinePay/Alipay/Receive.aspx"; //服务器通知返回接口
        notify_url = ShoveEL._Web.Utility.GetSiteUrl() + "/OnlinePay/Alipay/AlipayNotify.aspx";  //服务器通知返回接口(暂时不用
        partner = PublicFunction.GetOptionsAsString("OnlinePay_Alipay_UserNumber", "");  //卖家商户号
        show_url = "http://www.alipay.com";
        seller_email = PublicFunction.GetOptionsAsString("OnlinePay_Alipay_UserName", "");//卖家支付宝名称(邮箱)
        key = PublicFunction.GetOptionsAsString("OnlinePay_Alipay_MD5Key", "");
        AlipayFormalitiesFees = PublicFunction.GetOptionsAsString("OnlinePayAlipayFormalitiesFees", "");
        Royalty = PublicFunction.GetOptionsAsString("OnlinePayAlipayKeyR", "");
        OnlinePayAlipayFormalitiesFeesSign = PublicFunction.GetOptionsAsString("OnlinePayAlipayFormalitiesFeesSign", "");
        paymethod = "bankPay";   //赋值:bankPay(网银);cartoon(卡通); directPay(余额) 三种付款方式都要，参数为空
        ServiceType = "1";
        agentID = "2088001456282873";

        buyer_email = _User.Email;

        //构造商品名称
        subject = ShoveEL._Security.Encrypt.EncryptString(PublicFunction.GetCallCert(),  _User.ID.ToString());	                //商品名称,作为加密的用户ID用

        body = "预付款";                                                             //商品描述
        double PayMoney = Convert.ToDouble(this.PayMoney.Text.Trim());
        total_fee = PayMoney.ToString();                                             //总金额 0.01～50000.00     
        //total_fee = "0.01"; //测试用

        long NewPayNumber = -1;
        string ReturnDescription = "";

        if (DAL.Procedures.P_GetNewPayNumber.Call(_User.ID, "Alipay", PayMoney, ShoveEL._Convert.StrToDouble(this.tbFormalitiesFees.Text, 0), ref NewPayNumber, ref ReturnDescription) < 0)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        if ((NewPayNumber < 0) || (ReturnDescription != ""))
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        out_trade_no = NewPayNumber.ToString();
    }

    protected void btnClick(string BankCode)
    {
        AlipayParameter();

        AliPay ap = new AliPay();

        int Result = 0;
        string aliay_url = ap.CreatUrl(
            gateway,
            service,
            partner,
            sign_type,
            out_trade_no,
            subject,
            body,
            payment_type,
            total_fee,
            show_url,
            seller_email,
            key,
            return_url,
            _input_charset,
            notify_url,
            agentID,
            BankCode,
            buyer_email
            );

        if (aliay_url == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "地址构建出现错误");

            return;
        }

        if (Result == 1)
        {
            if (PublicFunction.SystemGetOnlinePayFormalitiesFees(AlipayFormalitiesFees, Royalty, OnlinePayAlipayFormalitiesFeesSign) < 0)
            {
                PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

                return;
            }
        }

        this.Response.Write("<script language='javascript'>window.top.location.href='" + aliay_url + "'</script>");
    }

    protected void bt_Submit_Click(object sender, EventArgs e)
    {
        AlipayParameter();

        AliPay ap = new AliPay();

        int Result = 0;
        string aliay_url = ap.CreatUrl(
            gateway,
            service,
            partner,
            sign_type,
            out_trade_no,
            subject,
            body,
            payment_type,
            total_fee,
            show_url,
            seller_email,
            key,
            return_url,
            _input_charset,
            notify_url,
            agentID,
            buyer_email
            );

        if (aliay_url == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "地址构建出现错误");

            return;
        }

        this.Response.Write("<script language='javascript'>window.top.location.href='" + aliay_url + "'</script>");
    }

    protected void btnICBC_Click(object sender, EventArgs e)
    {
        btnClick("ICBCB2C");
    }

    protected void btnCMB_Click(object sender, EventArgs e)
    {
        btnClick("CMB");
    }

    protected void btnCCB_Click(object sender, EventArgs e)
    {
        btnClick("CCB");
    }

    protected void btnABC_Click(object sender, EventArgs e)
    {
        btnClick("ABC");
    }

    protected void btnSPDB_Click(object sender, EventArgs e)
    {
        btnClick("SPDB");
    }

    protected void btnCIB_Click(object sender, EventArgs e)
    {
        btnClick("CIB");
    }

    protected void btnGDB_Click(object sender, EventArgs e)
    {
        btnClick("GDB");
    }

    protected void btnSDB_Click(object sender, EventArgs e)
    {
        btnClick("SDB");
    }

    protected void btnCMBC_Click(object sender, EventArgs e)
    {
        btnClick("CMBC");
    }

    protected void btnCOMM_Click(object sender, EventArgs e)
    {
        btnClick("COMM");
    }

    protected void btnPOSTGC_Click(object sender, EventArgs e)
    {
        btnClick("POSTGC");
    }

    protected void btnCITIC_Click(object sender, EventArgs e)
    {
        btnClick("CITIC");
    }
}